<template>
  <div class="zc-select-modal modal-mask">
    <div class="zc-select-wrap modal-content">
      <div class="title-wrap flx-justify-between">
        <div class="title">{{ title }}</div>
        <div class="close pointer" @click="close">
          <el-icon>
            <Close />
          </el-icon>
        </div>
      </div>

      <div class="zc-select-content flx-justify-between">
        <div class="left">
          <el-input v-model="searchKeywords" placeholder="请输入姓名进行搜索">
            <template #prefix>
              <el-icon class="el-input__icon">
                <Search />
              </el-icon>
            </template>
          </el-input>
          <div class="select-card">
            <div class="header flx-justify-between">
              <div v-if="!searchKeywords" class="sub-title">选择人员</div>
              <div class="tabs">
                <div
                  v-for="(tab, index) in tabList"
                  :key="index"
                  :class="{ active: currentTab == index }"
                  class="item"
                  @click="tabsChange(index)"
                >
                  {{ tab }}
                </div>
              </div>
            </div>
            <div class="slect-content flx">
              <div v-if="(currentTab == 0 || currentTab == 1) && !searchKeywords" class="left-list">
                <el-scrollbar class="scrollbar">
                  <!-- 选择角色 -->
                  <template v-if="currentTab == 0">
                    <div
                      v-for="(item, index) in roleList"
                      :key="index"
                      :class="{ active: currentRole == item.id }"
                      class="list-item sle"
                      @click="currentRole = item.id"
                    >
                      {{ item.name }}
                    </div>
                  </template>

                  <!-- 选择部门 -->
                  <template v-if="currentTab == 1">
                    <el-tree :data="data" show-checkbox @check-change="handleCheckChange" />
                  </template>
                </el-scrollbar>
              </div>
              <div class="right-member">
                <el-scrollbar class="scrollbar">
                  <el-checkbox
                    v-if="multiple && memberList.length"
                    v-model="checkAll"
                    :indeterminate="isIndeterminate"
                    @change="handleCheckAllChange"
                  >
                    全选
                  </el-checkbox>
                  <el-checkbox-group v-model="checkedMember" @change="handleCheckedMemberChange">
                    <el-checkbox
                      v-for="(member, index) in memberList"
                      :key="index"
                      :disabled="checkedMember.length && !checkedMember.includes(member.id)"
                      :label="member.id"
                      :value="member.id"
                    >
                      {{ member.name }}
                    </el-checkbox>
                  </el-checkbox-group>

                  <el-empty v-if="!memberList || !memberList.length" description="未搜索到相关人员"></el-empty>
                </el-scrollbar>
              </div>
            </div>
          </div>
        </div>
        <div class="right">
          <div class="right-top flx-justify-between">
            <div class="selected text-color-6">
              已选(<span class="text-color-8">{{ checkedMember.length }}</span
              ><span class="text-color-4">/{{ multiple ? 100 : 1 }}</span
              >)
            </div>
            <div class="text-primary pointer" @click="emptied">清空</div>
          </div>
          <el-scrollbar class="flx-1">
            <div class="selected-list">
              <div v-for="(item, index) in checkedMember" :key="index" class="item text-color-6 flx-align-center">
                {{ findeMemberName(item) }}
                <el-icon @click="delMember(index)">
                  <Close />
                </el-icon>
              </div>
            </div>
          </el-scrollbar>
          <div class="footer flx-justify-end">
            <el-button @click="close">取消</el-button>
            <el-button v-throttle="submit" :disabled="!checkedMember.length" type="primary">确定</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { Close, Search } from "@element-plus/icons-vue";
import { computed, defineEmits, defineProps, ref } from "vue";

const emit = defineEmits(["close", "selectMember"]);
const prop = defineProps({
  // 标题
  title: {
    type: String,
    default: "选择成员"
  },

  // 是否多选
  multiple: {
    type: Boolean,
    default: true
  }
});

// 搜索关键词
const searchKeywords = ref("");
const currentTab = ref(0);
const tabList = ["角色", "部门", "用户"];
// 角色列表
const roleList = ref([
  { id: 1, name: "销售总监" },
  { id: 2, name: "知产顾问" },
  { id: 3, name: "销售组长" },
  { id: 4, name: "推广主管" },
  { id: 5, name: "知产经理" },
  { id: 6, name: "知产材料员" },
  { id: 7, name: "知产主管" }
]);

// 切换角色 部门和用户
const tabsChange = index => {
  currentTab.value = index;
};

// 已选中的角色
const currentRole = ref("");

// 部门列表
const data = [
  {
    label: "Level one 1",
    children: [
      {
        label: "Level two 1-1",
        children: [
          {
            label: "Level three 1-1-1"
          }
        ]
      }
    ]
  },
  {
    label: "Level one 2",
    children: [
      {
        label: "Level two 2-1",
        children: [
          {
            label: "Level three 2-1-1"
          }
        ]
      },
      {
        label: "Level two 2-2",
        children: [
          {
            label: "Level three 2-2-1"
          }
        ]
      }
    ]
  },
  {
    label: "Level one 3",
    children: [
      {
        label: "Level two 3-1",
        children: [
          {
            label: "Level three 3-1-1"
          }
        ]
      },
      {
        label: "Level two 3-2",
        children: [
          {
            label: "Level three 3-2-1"
          }
        ]
      }
    ]
  }
];

// 选择部门
const handleCheckChange = (data, checked, indeterminate) => {
  console.log(data, checked, indeterminate);
};

// 用户列表
const memberList = ref([
  { id: 1, name: "張三" },
  { id: 2, name: "李四" },
  { id: 3, name: "铁柱" },
  { id: 4, name: "王二" },
  { id: 5, name: "钢蛋" },
  { id: 6, name: "来福" },
  { id: 7, name: "欢欢" }
]);

// 已选中的人员
const checkedMember = ref([]);
// 所有人员的id
const memberIds = [1, 2, 3, 4, 5, 6, 7];

// 是否全选
const checkAll = ref(false);
// 是否半选
const isIndeterminate = ref(false);

// 全选
const handleCheckAllChange = (val: boolean) => {
  checkedMember.value = val ? JSON.parse(JSON.stringify(memberIds)) : [];
  isIndeterminate.value = false;
};

// 单选
const handleCheckedMemberChange = (value: string[]) => {
  const checkedCount = value.length;
  checkAll.value = checkedCount === memberIds.length;
  isIndeterminate.value = checkedCount > 0 && checkedCount < memberIds.length;
};

// 通过id 查找对应的用户名
const findeMemberName = computed(() => {
  return (id: string) => {
    return memberList.value.find((item: any) => item.id == id).name;
  };
});

// 清空
const emptied = () => {
  checkAll.value = false;
  isIndeterminate.value = false;
  checkedMember.value = [];
};
// 删除已选
const delMember = index => {
  checkedMember.value.splice(index, 1);
  if (checkedMember.value.length == 0) {
    isIndeterminate.value = false;
    console.log(isIndeterminate.value);
  } else {
    checkAll.value = false;
    isIndeterminate.value = true;
  }
};

// 关闭
const close = () => {
  emit("close");
};

// 确定
const submit = () => {
  emit("selectMember", checkedMember.value);
};
</script>

<style lang="scss" scoped>
.zc-select-modal {
  .zc-select-wrap {
    width: 600px;

    .zc-select-content {
      padding-top: 11px;
      align-items: flex-start;

      .left {
        width: 372px;

        .select-card {
          border: 1px solid rgba(186, 191, 197, 0.4);
          border-radius: 4px;
          padding: 8px;
          height: 404px;
          margin-top: 10px;
          display: flex;
          flex-direction: column;

          .header {
            height: 32px;
            display: flex;

            .sub-title {
              margin-right: 108px;
            }

            .tabs {
              flex: 1;
              display: flex;
              border: 1px solid #f4f4f5;
              border-radius: 4px;
              line-height: 32px;

              .item {
                font-size: 12px;
                flex: 1;
                text-align: center;
                border-right: 1px solid #f4f4f5;
                cursor: pointer;

                &:hover,
                &.active {
                  color: var(--el-color-primary);
                }

                &:last-child {
                  border-right: none;
                }
              }
            }
          }

          .scrollbar {
            height: 100%;
            overflow-x: hidden;
          }

          .slect-content {
            flex: 1;
            padding-top: 8px;
            overflow: hidden;

            .left-list {
              width: 200px;

              .list-item {
                height: 36px;
                line-height: 36px;
                padding: 0 16px;
                cursor: pointer;

                &.active,
                &:hover {
                  background: #f5f7fa;
                }
              }

              :deep(.el-tree-node__content) {
                padding-bottom: 10px;
              }
            }

            .right-member {
              flex: 1;
              margin-left: 18px;

              .el-checkbox {
                height: 36px;
                width: 100%;
                margin: 0;
              }
            }
          }
        }
      }

      .right {
        height: 464px;
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 20px;

        .right-top {
          height: 22px;
          padding: 5px 0;
          border-bottom: 1px solid rgba(186, 191, 197, 0.4);
        }

        .selected-list {
          display: flex;
          flex-wrap: wrap;
          margin-left: -8px;
          margin-top: 10px;

          .item {
            height: 30px;
            background: #f5f6f7;
            border-radius: 4px;
            padding: 0 11px 0 8px;
            margin-left: 8px;
            margin-bottom: 8px;
            cursor: pointer;

            i {
              vertical-align: middle;
              margin-top: 2px;
              margin-left: 8px;
            }
          }
        }
      }
    }
  }
}
</style>
